
ac.buff('爬塔挑战')
{
    on_add = function(self)
        local unit = self.owner
        local player = unit.owner_player
        unit['可挑战'] = true
        unit.ui_title = ('%s(|cfffff000A我挑战|r)|n击杀掉落|cff00ff00木材|r'):format(unit.title or unit.name)
        unit:set_owner(ac.player[16])
        unit:add_restriction '免伤'
        unit:add_restriction '幽灵'
        self:gc(unit:event '单位-被挑战'(function(trg,_,source)
            self:remove()

            unit:add_buff '挑战限时'{time = 20}
            unit:issue_order('attack',source)
        end))
        self:gc(function()
            unit['可挑战'] = nil
            unit:set_owner(player.enemy_player)
            unit:remove_restriction '免伤'
        end)
    end,
}

ac.game:event '玩家-刷新爬塔'(function(trg,player)
    local count = player:get_data('深渊爬塔层数',0)
    local angle = player.enemy_point / player.point1
    local point = player.enemy_point - {angle + count*72,600}
    local level = 50 * (count + 1)
    local unit = player:create_enemy('深渊战士',point,level)
    unit.title = ('深渊第%s层'):format(count + 1)
    unit:add_buff '爬塔挑战'{}
    unit:event '单位-即将移除'(function()
        if unit.defeated==nil then
            player:notify('玩家-刷新爬塔',player)
        end
    end)
    unit:event '单位-即将死亡'(function()
        local count = player:get_data('深渊爬塔层数',0) + 1
        unit.defeated = true

        player:add_icon('木材',20 * count,unit)
        player:set_data('深渊爬塔层数',count)
        if count<30 then
            player:notify('玩家-刷新爬塔',player)
        end

        local start = unit:get_impact_point()
        player:notify('玩家-刷新宝箱',player,count,point,start)
    end)
end)

local list = {
    {'金币',1000,2000},
    {'木材',20,40},
    {'杀敌',100,200},
}
ac.game:event '玩家-刷新宝箱'(function(trg,player,count,point,start)
    local point = point or player.enemy_point - {math.random(360),math.random(300,600)}
    local unit = player:create_enemy('深渊宝箱',point)
    unit:add_buff '分离演出'{time = 1,start = start}
    unit:add_buff '挑战限时'{time = 120}
    unit:set('移动速度',0)
    unit:event '单位-死亡'(function()
        local data = table.random(list)
        local key = data[1]
        local num = math.random(data[2],data[3])
        player:add_icon(key,num,unit)
    end)
end)

ac.game:event '游戏-开始刷怪'(function(trg,self)
    trg:remove()
    ac.each_player(function(player)
        player:notify('玩家-刷新爬塔',player)
    end)
end)